/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.unix;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.DatagramSocket;

import fr.x9c.cadmium.kernel.Channel;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.FalseExit;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;

/**
 * This class provides implementation for 'unix_socket' primitive.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
@PrimitiveProvider
public final class Socket {

    /** Identifier for stream sockets. */
    private static final int SOCKET_STREAM = 0;

    /** Identifier for datagram sockets. */
    private static final int SOCKET_DATAGRAM = 1;

    /**
     * No instance of this class.
     */
    private Socket() {
    } // end empty constructor

    /**
     * Creates a socket.
     * @param ctxt context
     * @param domain socket domain, does not support <i>unix</i> domain
     * @param type <ul>
     *               <li><tt>O</tt>: stream socket</li>
     *               <li><tt>1</tt>: datagram socket</li>
     *               <li><tt>2</tt>: raw socket - not supported</li>
     *               <li><tt>3</tt>: sequenced packet socket - not supported</li>
     *             </ul>
     * @param proto ignored
     * @return socket file descriptor
     * @throws Fail.Exception if socket creation fails
     */
    @Primitive
    public static Value unix_socket(final CodeRunner ctxt,
                                    final Value domain,
                                    final Value type,
                                    final Value proto)
        throws Fail.Exception, FalseExit {
        if (domain == Value.ZERO) {
            Unix.fail(ctxt, "socket", "unsupported socket domain");
            return Value.UNIT; // never reached
        } // end if
        try {
            switch (type.asLong()) {
            case Socket.SOCKET_STREAM:
                return Value.createFromLong(ctxt.getContext().addChannel(new Channel()));
            case Socket.SOCKET_DATAGRAM:
                return Value.createFromLong(ctxt.getContext().addChannel(new Channel(new DatagramSocket())));
            default:
                Unix.fail(ctxt, "socket", "unsupported socket type");
                return Value.UNIT; // never reached
            } // end switch
        } catch (final InterruptedIOException iioe) {
            final FalseExit fe =
                FalseExit.createFromContext(ctxt.getContext());
            fe.fillInStackTrace();
            throw fe;
        } catch (final IOException ioe) {
            Unix.fail(ctxt, "socket", ioe);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'unix_socket(CodeRunner, Value, Value, Value)'

} // end class 'Socket'
